<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Invoice_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $process = '';
	var $created_time = 0;
	var $expire_date = 0;
	var $status = '';
	var $paid_date = 0;
	var $company = '';
	var $description = '';
	var $history_id = 0;	
	var $user_id = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setProcess($input){
		$this->process = $input;
	}
	function setCreatedTime($input){
		$this->created_time = $input;
	}
	function setExpireDate($input){
		$this->expire_date = $input;
	}
	function setStatus($input){
		$this->status = $input;
	}
	function setPaidDate($input){
		$this->paid_date = $input;
	}
	function setCompany($input){
		$this->company = $input;
	}
	function setDescription($input){
		$this->description = $input;
	}
	
	function getId(){
		return $this->id;
	}
	function getProcess(){
		return $this->process;
	}
	function getCreatedTime(){
		return $this->created_time;
	}
	function getExpireDate(){
		return $this->expire_date;
	}
	function getStatus(){
		return $this->status;
	}
	function getPaidDate(){
		return $this->paid_date;
	}
	function getCompany(){
		return $this->company;
	}
	function getDescription(){
		return $this->description;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_INVOICE)->result();
		}else if($cmd == 'history_id'){
			$result= $ci->db->where('history_id',$input)->get(TBL_INVOICE)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->process = $result[0]->process;
			$this->created_time = $result[0]->created_time;
			$this->expire_date = $result[0]->expire_date;
			$this->status = $result[0]->status;
			$this->paid_date = $result[0]->paid_date;
			$this->company = $result[0]->company;
			$this->description = $result[0]->description;
			$this->history_id = $result[0]->history_id;
			$this->user_id = $result[0]->user_id;
			return $this;
		}
		return false;
	}
	
	function getList($limit,$base_where){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(isset($base_where['ref'])){
			$ci->db->like('description', $base_where['ref']);
		}
		if(isset($base_where['from'])){
			$ci->db->where('created_time >= ', strtotime($base_where['from']));
		}
		if(isset($base_where['to'])){
			$ci->db->where('created_time <= ', strtotime($base_where['to']));
		}
		if(count($limit) >0){
			$result = $ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('created_time', 'DESC');
		return $ci->db->get(TBL_INVOICE)->result();
	}
	function count($base_where){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(isset($base_where['ref'])){
			$ci->db->like('description', $base_where['ref']);
			$ci->db->or_like('company', $base_where['ref']);
		}
		if(isset($base_where['from'])){
			$ci->db->where('created_time > ', strtotime($base_where['from']));
		}
		if(isset($base_where['to'])){
			$ci->db->where('created_time < ', strtotime($base_where['to']));
		}
		return $ci->db->count_all_results(TBL_INVOICE);
	}
	
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('process',$this->getProcess());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('expire_date',$this->getExpireDate());
		$ci->db->set('status',$this->getStatus());
		$ci->db->set('paid_date',$this->getPaidDate());
		$ci->db->set('company',$this->getCompany());
		$ci->db->set('description',$this->getDescription());
		$ci->db->insert(TBL_INVOICE);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('process',$this->getProcess());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('expire_date',$this->getExpireDate());
		$ci->db->set('status',$this->getStatus());
		$ci->db->set('paid_date',$this->getPaidDate());
		$ci->db->set('company',$this->getCompany());
		$ci->db->set('description',$this->getDescription());
		return $ci->db->where('id', $this->id)->update(TBL_INVOICE);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_INVOICE);
	}
	
	function countActiveInvoice(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		return $ci->db->count_all_results(TBL_INVOICE);
	}
	
	public function getInvoiceListOfDay($date){
		$ci=& get_instance();
		//$ci->db->where('created_time', strtotime($date));
		$ci->db->where('created_time >= ', mktime (0, 0, 0, date("m",strtotime($date)), date("d",strtotime($date)), date("Y",strtotime($date))));
		$ci->db->where('created_time <= ',  mktime (23, 59, 59, date("m",strtotime($date)), date("d",strtotime($date)), date("Y",strtotime($date))));
		
		$ci->db->order_by('created_time', 'DESC');
		
		return $ci->db->get(TBL_INVOICE)->result();
	}
	public function getInvoiceListOfMonth($month, $year){
		$ci=& get_instance();
		
		$ci->db->where('created_time >= ', firtDayOfMonthTimestamp($month, $year));
		$ci->db->where('created_time <= ', lastDayOfMonthTimestamp($month, $year));
		$ci->db->order_by('created_time', 'DESC');
		
		return $ci->db->get(TBL_INVOICE)->result();
	}
	public function getInvoiceListOfYear($year){
		$ci=& get_instance();
		
		$ci->db->where('created_time >= ', firtDayOfMonthTimestamp(1, $year));
		$ci->db->where('created_time <= ', lastDayOfMonthTimestamp(12, $year));
		$ci->db->order_by('created_time', 'DESC');
		
		return $ci->db->get(TBL_INVOICE)->result();
	}
	
	
}